// 入口文件
import Vue from 'vue'
//1.1导入路由的包
import VueRouter from 'vue-router'
//1.2 安装路由
Vue.use(VueRouter)

//注册Vuex 
import Vuex from 'vuex'
Vue.use(Vuex)

//每次进入网站 肯定会调用main.js
var car=JSON.parse(localStorage.getItem('car') || '[]')

var store=new Vuex.Store({
  state:{//this.$store.state.***
    car:car //购物车数据存储当中 {id:商品ID,count:商品数量,price:商品价格,selected:false}

  },
  mutations:{ //this.store.commit('方法名','唯一参数')
    addToCar(state,goodsInfo){
      var flag=false;
      state.car.some(item=>{
        if(item.id==goodsInfo.id){
          item.count+=parseInt(goodsInfo.count)
          flag=true
          return true
        }
      })
      if(!flag){
        state.car.push(goodsInfo)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateGoodsInfo(state,goodsinfo){
      //修改购物车中商品的值
      state.car.some(item=>{
        if(item.id==goodsinfo.id){
          item.count=parseInt(goodsinfo.count)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    removeFormCar(state,id){
      state.car.some((item,i)=>{
        if(item.id==id){
          state.car.splice(i,1)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updateGoodsSeclected(state,info){
      state.car.some(item=>{
        if(item.id==info.id){
          item.selected=info.selected
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    }

  },
  getters:{// this.$store.getters.***
    //相当于计算属性,也相当于filters
    getAllcount(state){
      var c=0;
      state.car.forEach(item=>{
        c+=item.count
      })
      return c
    },
    getGoodsCount(state){
      var o={}
      state.car.forEach(item=>{
        o[item.id]=item.count
      })
      return  o
    },
    getGoodsSelected(state){
      var o={}
      state.car.forEach(item=>{
        o[item.id]=item.selected
      })
      return o
    },
    getGoodsCountAndAmount(state){
      var o={
        count:0,
        amount:0
      }
      state.car.forEach(item=>{
        if(item.selected){
          o.count+=item.count,
          o.amount+=item.price+item.count
        }
      })
      return o
    }
   

  }

})

import app from './App.vue'
//时间插件
import moment from 'moment'
Vue.filter('dateFormat',function(data,params){
    return moment(data).format(params)
})

import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'http://www.liulongbin.top:3005';
//头部 
// import { Header,Swipe, SwipeItem,Button,Lazyload } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name,Button);
// Vue.use(Lazyload)



import Mint from 'mint-ui';
Vue.use(Mint);
import 'mint-ui/lib/style.css'

// 缩略图
import VuePreview from 'vue-preview'

Vue.use(VuePreview)
Vue.use(VuePreview, {
    mainClass: 'pswp--minimal--dark',
    barsSize: {top: 0, bottom: 0},
    captionEl: false,
    fullscreenEl: false,
    shareEl: false,
    bgOpacity: 0.85,
    tapToClose:false,
    tapToToggleControls: false
  })


  

Vue.http.options.emulateHTTP = true;


import router from './router.js'


import './lib/css/mui.min.css'
import './lib/css/icons-extra.css'
import './lib/fonts/mui-icons-extra.ttf'


 var vm=new Vue({
    el:'#app',
    data:{},
    render:c=> c(app),
    router,
    store
})